package SeqList;
import TripleTable.ThreeTuple;

public class SeqList<T> implements IList<T> {
    ThreeTuple[] element;
    int len;

    public SeqList(int size){
        this.element = new ThreeTuple[size];
        this.len = 0;
    }

    @Override
    public boolean isEmpty() {
        return this.len == 0;
    }

    @Override
    public int length() {
        return this.len;
    }

    @Override
    public int getRow(int i) {
        return this.element[i].getRow();
    }

    @Override
    public int getCol(int i) {
        return this.element[i].getCol();
    }

    @Override
    public int getWeight(int i) {
        return this.element[i].getWeight();
    }

    @Override
    public ThreeTuple get(int i) {
        return element[i];
    }

    @Override
    public void setRow(int i, int x) {
        if(i >= 0 && i < this.len){
            this.element[i].setRow(x);
        }else{
            throw new IndexOutOfBoundsException(i + "");
        }
    }

    @Override
    public void setCol(int i, int x) {
        if(i >= 0 && i < this.len){
            this.element[i].setCol(x);
        }else{
            throw new IndexOutOfBoundsException(i + "");
        }
    }

    @Override
    public void setWeight(int i, int x) {
        if(i >= 0 && i < this.len){
            this.element[i].setWeight(x);
        }else{
            throw new IndexOutOfBoundsException(i + "");
        }
    }

    @Override
    public void set(int i,ThreeTuple x) {
        this.element[i] = x;
    }

    @Override
    public void insert(int i, ThreeTuple x) {
        if (x == null){
            return;
        }
        if(i < 0){//阻止插入到错误下标的值
            i = 0;
        }
        if (this.len == element.length){//满串扩容
            ThreeTuple[] temp = this.element;
            this.element = new ThreeTuple[temp.length + 1];
            for(int j = 0;j < temp.length; j++){
                this.element[j] = temp[j];
            }
        }
        for(int j = this.len - 1;j >= i;j--){
            this.element[j + 1] = this.element[j];
        }
        element[i] = x;
        this.len++;
    }

    @Override
    public void lengthen(ThreeTuple x) {
        insert(this.len , x);
    }

    @Override
    public void remove(int i) {
        if(this.len == 0 || i < 0 || i >= this.len){
            System.out.println("delete position=" + i + ",is out index.");
            return;
        }
        ThreeTuple deleted = this.element[i];
        System.out.println("node:" + deleted + ",is deleted from seqlist.");
        for(int j = i; j < this.len - 1; j++){//把i后的数据元素依次前移
            this.element[j] = this.element[j + 1];
        }
        this.element[this.len - 1] = null;
        this.len--;
        return;
    }

    @Override
    public void removeAll() {//删除线性表内所有值
        this.len = 0;
    }

    @Override
    public int search(T key) {
        for(int i = 0; i < this.len; i++){
            if(key.equals(this.element[i])){//搜寻地址与key相同的元素
                return i;
            }
        }
        return -1;
    }

    @Override
    public ThreeTuple[] getElements() {
        return this.element;
    }
}
